package com.loyal.mwm.ui

import android.graphics.Bitmap
import android.net.Uri
import android.view.View
import com.loyal.mwm.R
import com.loyal.mwm.base.BaseActivity
import com.loyal.mwm.databinding.ActivitySimpleUcropBinding
import com.loyal.mwm.util.DisplayUtil
import com.loyal.mwm.util.MemoryUtil
import com.loyal.mwm.util.ImageUtil
import com.yalantis.ucrop.UCrop
import java.io.File

class SimpleUCropActivity : BaseActivity<ActivitySimpleUcropBinding>(),
    View.OnClickListener {

    override fun actLayoutRes(): Int {
        return R.layout.activity_simple_ucrop
    }

    override fun afterOnCreate() {
        binding.drawable = ImageUtil.getBackground(this)
        initViews()
    }

    private fun initViews() {
        val toolbar = binding.toolbar.root
        toolbar.title = "图片裁剪"
        setSupportActionBar(toolbar)
        binding.btnTest.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.btn_test -> {
                val size: Int = DisplayUtil.dip2px(this, 200f)
                val resUrl = "http://star.xiziwang.net/uploads/allimg/140512/19_140512150412_1.jpg"
                val options = UCrop.Options()
                options.setCompressionFormat(Bitmap.CompressFormat.JPEG)
                options.setCompressionQuality(100)
                val destinationUri = Uri.fromFile(File(MemoryUtil.path_temp, "t_icon.jpeg"))
                var uCrop: UCrop = UCrop.of(Uri.parse(resUrl), destinationUri)
                uCrop = uCrop.withAspectRatio(size.toFloat(), size.toFloat())
                uCrop = uCrop.withOptions(options)
                uCrop.start(this)
            }
        }
    }
}